package com.test.service.impl;

import com.test.common.ErrorCode;
import com.test.exception.BusinessException;
import com.test.mapper.MultipleQuestionMapper;
import com.test.mapper.SingleQuestionMapper;
import com.test.mapper.TrueFalseQuestionMapper;
import com.test.mapper.UserMapper;
import com.test.model.dto.TFStatDTO;
import com.test.model.dto.UserDTO;
import com.test.model.dto.UserInfoDTO;
import com.test.model.dto.page.QuestionPageQuery;
import com.test.model.dto.wrongTime.WrongTimeCountsDTO;
import com.test.model.pojo.MultipleQuestion;
import com.test.model.pojo.SingleQuestion;
import com.test.model.pojo.TrueFalseQuestion;
import com.test.model.vo.UserVO;
import com.test.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

import java.util.List;
import java.util.Objects;

import static com.test.constant.UserConstant.USER_LOGIN_STATE;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public UserDTO getUserByName(String username) {
        return userMapper.getUser(username);
    }

    @Override
    public TFStatDTO getUserToQuestionStateById(Integer id) {
        return userMapper.getUserToQuestionStateById(id);
    }

    @Override
    public UserDTO getUserById(Integer id) {
        return userMapper.getUserById(id);
    }

    @Override
    public UserInfoDTO getUserState(Integer id) {
        UserInfoDTO states = userMapper.getUserStates(id);
//        System.out.println(states.getCorrectNums());
        return states;
    }

    @Override
    public String getLogin(String username, String password, HttpServletRequest request) {
        // 1. 校验
        if (StringUtils.isAnyBlank(username, password)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数为空");
        }
        UserDTO user = userMapper.getUser(username);
        if(user==null){
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR,"未找到该用户！");
        }
        if(!user.getPassword().equals(password)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "密码错误！");
        }
        request.getSession().setAttribute(USER_LOGIN_STATE,user.getId());
        return null;
    }

    @Autowired
    MultipleQuestionMapper multipleQuestionMapper;
    @Autowired
    SingleQuestionMapper singleQuestionMapper;
    @Autowired
    TrueFalseQuestionMapper trueFalseQuestionMapper;

    @Override
    public void updateUserQuestion(WrongTimeCountsDTO wrongTimeCountsDTO,HttpServletRequest request) {
        if(Objects.equals(wrongTimeCountsDTO.getType(), "ms")){
            multipleQuestionMapper.updateUserQuestion(wrongTimeCountsDTO);
        }
        if(Objects.equals(wrongTimeCountsDTO.getType(),"tf")){
            trueFalseQuestionMapper.updateUserQuestion(wrongTimeCountsDTO);
        }
        if(Objects.equals(wrongTimeCountsDTO.getType(),"ss")){
            singleQuestionMapper.updateUserQuestion(wrongTimeCountsDTO);
        }
    }

    @Override
    public List<SingleQuestion> getSingleQuestionPage(String search, RowBounds rowBounds,HttpServletRequest request) {
        // 调用MyBatis的Mapper方法执行查询
        Integer userId = (Integer)request.getSession().getAttribute(USER_LOGIN_STATE);
        QuestionPageQuery questionPageQuery = new QuestionPageQuery(search,rowBounds,userId);
        List<SingleQuestion> singleQuestionPage = singleQuestionMapper.getSingleQuestionPage(questionPageQuery);
        return singleQuestionPage;
    }

    @Override
    public String getExit(HttpServletRequest request) {
        if (request.getSession().getAttribute(USER_LOGIN_STATE) == null) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "未登录");
        }
        request.getSession().removeAttribute(USER_LOGIN_STATE);
        return null;
    }

    @Override
    public List<MultipleQuestion> getMultiQuestionPage(String search, RowBounds rowBounds, HttpServletRequest request) {
        Integer userId = (Integer)request.getSession().getAttribute(USER_LOGIN_STATE);
        QuestionPageQuery questionPageQuery = new QuestionPageQuery(search,rowBounds,userId);
        List<MultipleQuestion> multiQuestionPage = multipleQuestionMapper.getMultiQuestionPage(questionPageQuery);
        return multiQuestionPage;
    }

    @Override
    public List<TrueFalseQuestion> getTrueFalseQuestionPage(String search, RowBounds rowBounds, HttpServletRequest request) {
        Integer userId = (Integer)request.getSession().getAttribute(USER_LOGIN_STATE);
        QuestionPageQuery questionPageQuery = new QuestionPageQuery(search,rowBounds,userId);
        List<TrueFalseQuestion> trueFalseQuestions = trueFalseQuestionMapper.getTrueFalseQuestionPage(questionPageQuery);
        return trueFalseQuestions;
    }

}
